/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhaolang.qogir.ui;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.zhaolang.qogir.R;
import com.zhaolang.qogir.cache.SharePreCacheHelper;
import com.zhaolang.qogir.domain.Login;
import com.zhaolang.qogir.domain.Login.LoginReq;
import com.zhaolang.qogir.domain.ReqFactory;
import com.zhaolang.qogir.ui.manage.FragmentManage;
import com.zhaolang.qogir.ui.processed.FragmentProcessed;
import com.zhaolang.qogir.ui.settings.FragmentSettings;
import com.zhaolang.qogir.util.EMChatUtils;
import com.zhaolang.qogir.util.ReleaseUtils;
import com.zhaolang.qogir.util.Slog;
import com.zhaolang.qogir.wifi.AllQogirManager;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;
import com.zhaolang.qogir.BuildConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import static android.util.Log.d;

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class ActivityTabs extends BaseFragmentActivity implements OnTabChangeListener {
    private FragmentTabHost mTabHost;
    private String mPreTabId;
    private boolean isOnPause = false;

    private static final long ENV_PARAM_TIME = 5000;
    private static final int MSG_GET_ENV_PARAM = 1;
    public static ActivityTabs activityTabs;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case MSG_GET_ENV_PARAM:
                    if (!isOnPause) {
                        AllQogirManager.getInstance(ActivityTabs.this)
                                .startGetEnvParamWhenFree();
                        mHandler.removeMessages(MSG_GET_ENV_PARAM);
                        mHandler.sendEmptyMessageDelayed(MSG_GET_ENV_PARAM,
                                ENV_PARAM_TIME);
                    }
                    break;
            }
        }

    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        d("III", "ActivityTabs onActivityResult req " + requestCode + ", res " + resultCode);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ui_tabs);

        UmengUpdateAgent.setUpdateCheckConfig(false);
        UmengUpdateAgent.setUpdateAutoPopup(false);
        // SharePreCacheHelper.setAppUpdateFlag(this, false);
        UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
            @Override
            public void onUpdateReturned(int updateStatus,
                                         UpdateResponse updateInfo) {
                switch (updateStatus) {
                    case UpdateStatus.Yes: // has update
                        Slog.d("有更新");
                        SharePreCacheHelper.setAppUpdateFlag(ActivityTabs.this,
                                true);
                        UmengUpdateAgent.showUpdateDialog(ActivityTabs.this,
                                updateInfo);
                        break;
                    case UpdateStatus.No: // has no update
                        SharePreCacheHelper.setAppUpdateFlag(ActivityTabs.this,
                                false);
                        Slog.d("没有更新");
                        // Toast.makeText(mContext, "没有更新",
                        // Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.NoneWifi: // none wifi
                        Slog.d("没有WI-FI");
                        // Toast.makeText(mContext, "没有wifi连接， 只在wifi下更新",
                        // Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.Timeout: // time out
                        Slog.d("超时");
                        // Toast.makeText(mContext, "超时",
                        // Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        UmengUpdateAgent.update(this);
        // EMChatUtils.startLogin(SharePreCacheHelper.getUserID(this));

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(),
                android.R.id.tabcontent/* , R.id.realtabcontent */);
        mTabHost.setOnTabChangedListener(this);

        // View[] indicato rViews = getIndicatorViews();
        FragmentInfo[] infos = getFragmentInfos();
        for (int i = 0; i < infos.length; i++) {
            FragmentInfo info = infos[i];
            mTabHost.addTab(
                    mTabHost.newTabSpec(info.tag).setIndicator(
                            getIndicatorView(info.iconRes, info.textRes)),
                    info.clazz, info.args);
        }

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
        activityTabs = this;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return super.onKeyUp(keyCode, event);
    }

    public void setCurrentTab(int index) {
        mTabHost.setCurrentTab(index);
    }

    @Override
    public void onTabChanged(String tabId) {
        if ("skintest".equals(tabId)) {

        } else if (!"skintest".equals(tabId) && "skintest".equals(mPreTabId)) {
            Context context = getApplicationContext();
            // WifiHelper.getInstance(context).endSkinTest(context);
            // AllQogirManager.getInstance(context).endSkinTest();
        }
        mPreTabId = tabId;
    }

    @Override
    public void onBackPressed() {
        if ("topic".equals(mTabHost.getCurrentTabTag())) {
            super.onBackPressed();
        } else {
            mTabHost.setCurrentTabByTag("topic");
        }
    }

    public void setHome(){
        mTabHost.setCurrentTabByTag("processed");
    }

    private void test() {
        if (!BuildConfig.DEBUG) {
            return;
        }
        mTabHost.setCurrentTab(0);
        // startLogin();
        testExportAssets();
    }

    private void startLogin() {
        Login l = ReqFactory.buildInterface(getApplicationContext(),
                Login.class);
        String mUserID = SharePreCacheHelper.getUserName(this);
        LoginReq bean = new LoginReq();
        bean.setUsername(mUserID);
        l.loginAsync(bean, null);
    }

    private void testExportAssets() {
        final String test_rgb = "rgb.jpg";
        final String test_pl = "pl.jpg";
        File dir = getCacheDir();

        File testRgb = new File(dir, test_rgb);
        File testPl = new File(dir, test_pl);
        if (testRgb.exists() && testPl.exists()) {
            Log.i("III", test_rgb + " and " + test_pl + " exists!");
            return;
        }

        AssetManager am = getAssets();
        try {
            int len = 0;
            byte[] buf = new byte[1024 * 5];
            InputStream ris = am.open("test/" + test_rgb);
            OutputStream ros = new FileOutputStream(testRgb);
            while ((len = ris.read(buf)) != -1) {
                ros.write(buf, 0, len);
            }
            ris.close();
            ros.close();
            Log.i("III", "rgb.jpg writed!");

            InputStream pis = am.open("test/" + test_pl);
            OutputStream pos = new FileOutputStream(testPl);
            while ((len = pis.read(buf)) != -1) {
                pos.write(buf, 0, len);
            }
            pis.close();
            pos.close();
            Log.i("III", "pl.jpg writed!");

            // am.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        isOnPause = false;
        mHandler.removeMessages(MSG_GET_ENV_PARAM);
        mHandler.sendEmptyMessageDelayed(MSG_GET_ENV_PARAM, ENV_PARAM_TIME);
        if (ReleaseUtils.DEBUG_REMOTE_MODE) {
            String userId = SharePreCacheHelper.getUserID(this);
//            if (!TextUtils.isEmpty(userId))
//                EMChatUtils.startLogin(this);
        } else {
//            EMChatUtils.startLogin(this);
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        isOnPause = true;
        mHandler.removeMessages(MSG_GET_ENV_PARAM);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    private View getIndicatorView(int iconRes, int textRes) {
        // final int tabLength = 4;
        // int[] iconRes = new int[] { R.drawable.ico_test,
        // R.drawable.ico_found,
        // R.drawable.ico_circle, R.drawable.ico_mine };
        // int[] textRes = new int[] { R.string.main_test, R.string.main_found,
        // R.string.main_circle, R.string.main_mine };
        // View[] indicators = new View[tabLength];

        LayoutInflater inflater = getLayoutInflater();

        // for (int i = 0; i < indicators.length; i++) {
        View indicator = inflater.inflate(R.layout.layout_tabs_indicator, null);
        ImageView icon = (ImageView) indicator.findViewById(android.R.id.icon1);
        TextView text = (TextView) indicator.findViewById(android.R.id.text1);
        icon.setImageResource(iconRes);
        text.setText(textRes);
        return indicator;
    }

    private FragmentInfo[] getFragmentInfos() {
        FragmentInfo[] infos = new FragmentInfo[3];
        //part for ms
        infos[0] = new FragmentInfo("processed", R.drawable.icon_processed,
                R.string.main_processed, FragmentProcessed.class, null);
        infos[1] = new FragmentInfo("manage", R.drawable.icon_manage,
                R.string.main_manage, FragmentManage.class, null);
        infos[2] = new FragmentInfo("settings", R.drawable.icon_setting,
                R.string.main_settings, FragmentSettings.class, null);
        return infos;
    }

    class FragmentInfo {
        String tag;
        int iconRes;
        int textRes;
        Class<? extends Fragment> clazz;
        Bundle args;

        public FragmentInfo(String tag, int iconRes, int textRes,
                            Class<? extends Fragment> clazz, Bundle args) {
            this.tag = tag;
            this.iconRes = iconRes;
            this.textRes = textRes;
            this.clazz = clazz;
            this.args = args;
        }

    }
}
